//
//  SinaRetweetView.m
//  oc微博
//
//  Created by 安陆与 on 16/1/24.
//  Copyright © 2016年 安陆与. All rights reserved.
//

#import "SinaRetweetView.h"
#import "SinaStatusViewModel.h"
#import "SinaStatusModel.h"
#import "SinaPictureView.h"
#import "SinaStatusModel.h"


// 只要添加了这个宏，就不用带mas_前缀
#define MAS_SHORTHAND
// 只要添加了这个宏，equalTo就等价于mas_equalTo
#define MAS_SHORTHAND_GLOBALS
//这个头文件一定要写在宏定义下面
#import "Masonry.h"

@interface SinaRetweetView()

@property (nonatomic, weak)UILabel *contentLabel;

@property (nonatomic, weak)SinaPictureView *pictureView;

@property (nonatomic, strong) MASConstraint *bottmConstraint;

@end
@implementation SinaRetweetView


-(void)setRetweetModel:(SinaStatusViewModel *)retweetModel{

    _retweetModel = retweetModel;
    
    self.contentLabel.text = retweetModel.model.retweeted_status.text;
    
    [self.bottmConstraint uninstall];
    
    if (retweetModel.model.retweeted_status.pic_urls) {
        [self needsUpdateConstraints];
        [self updateConstraintsIfNeeded];
        
        [self updateConstraints:^(MASConstraintMaker *make) {
        self.bottmConstraint = make.bottom.equalTo(_pictureView.bottom);
        
        }];
        
//        retweetModel.model.pic_urls
        NSMutableArray *array = [NSMutableArray array];
        
        for (NSDictionary *dic in retweetModel.model.retweeted_status.pic_urls) {
            
            NSString *str = dic[@"thumbnail_pic"];
            
            NSURL *url = [NSURL URLWithString:str];
            
            [array addObject:url];
        }
        
        self.pictureView.picture = array.copy;
//        self.pictureView.picture = retweetModel.model.retweeted_status.pic_urls;
        
        NSLog(@"^^^^^^^^%@",retweetModel.model.retweeted_status.pic_urls);

        
    }else{
        [self needsUpdateConstraints];
        [self updateConstraintsIfNeeded];
        
        [self updateConstraints:^(MASConstraintMaker *make) {
         self.bottmConstraint = make.bottom.equalTo(_contentLabel.bottom);
        }];
    }
}


-(instancetype)initWithFrame:(CGRect)frame{

    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    
    return self;
}


-(void)setupUI{

    UILabel *label = [[UILabel alloc]init];
    
    self.contentLabel = label;
    
    label.numberOfLines = 0;
    
    label.textColor = [UIColor darkGrayColor];
    
    [self addSubview:label];
    
    
    SinaPictureView *pictureView = [[SinaPictureView alloc]init];
    
    [self addSubview:pictureView];
    
    self.pictureView = pictureView;
    
    
    
//    约束
    [label makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.top);
        make.left.equalTo(self.left);
        make.width.equalTo(self.width);
//        make.bottom.equalTo(self.bottom);
    }];
    
    [pictureView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.bottom);
        make.left.equalTo(self.left).offset(5);
    }];
    
//
//    [self makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(label.bottom);
//    }];
}
@end






